package com.trans.core;

import com.trans.auxiliary.HandleFullRecord;
import com.trans.auxiliary.Params;
import com.trans.auxiliary.TranslatorAbleHelper;
import com.trans.auxiliary.TranslatorRecord;
import com.trans.handler.TranslateHandle;
import com.trans.utils.MapUtils;
import com.trans.utils.TypeUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

/**
 * @Author xwduo
 * @Description //TODO
 * @Date 2022-11-01 18:10
 */
@Slf4j
@SuppressWarnings("all")
public class MapJsonUtils {

    //仅限一层结构
    static Object recordMap(TranslatorRecord translatorRecord) {
        Map<String, HandleFullRecord> extactModelJsonMap = translatorRecord.getModelMap();
        //如果模板是空的
        if (null != extactModelJsonMap && !extactModelJsonMap.isEmpty()) {
            Map<String,Object> returnMap = new HashMap<>();
            Map data = (Map) translatorRecord.curObj;
            for (Object entryObj : data.entrySet()) {
                if(null != entryObj && entryObj instanceof Map.Entry) {
                    Map.Entry entry = (Map.Entry) entryObj;
                    Object key = entry.getKey();
                    Object value = data.get(key);
                    if(null != key ){
                        if(TranslatorAbleHelper.nullAble(value)){
                            String fieldName = String.valueOf(key);
                            if (value instanceof Collection) {
                                //Map里面假如是列表
                                Object o = TranslateUtils.searchTranslateData(translatorRecord.copy(value));
                                returnMap.put(fieldName, o);
                            } else if (value instanceof Map) {
                                //假如是个Map
                                Object translateData = MapJsonUtils.recordMap(translatorRecord.copy(value));
                                returnMap.put(fieldName, translateData);
                            } else if (TypeUtils.isCustomClass(value)) {
                                //假如是个自定义对象
                                TranslatorRecord subTranslatorRecord = translatorRecord.copyNewMap(value);
                                Map<Class<? extends TranslateHandle>, List<HandleFullRecord>> handleMap = TranslateUtils.doSearch(subTranslatorRecord);
                                MapUtils.combine(subTranslatorRecord.handleRecordMap, handleMap);
                                returnMap.put(fieldName, subTranslatorRecord.subEntry);
                            } else {
                                //包装类
                                //这里用了modelJSON
                                if (extactModelJsonMap.containsKey(fieldName)) {
                                    HandleFullRecord modelHandleRecord = extactModelJsonMap.get(fieldName);
                                    Params params = modelHandleRecord.params;
                                    Object fullObj = translatorRecord.fullObject;
                                    //使用注解的信息
                                    Class<? extends TranslateHandle>[] handleClasses = modelHandleRecord.translator.tranHandle();
                                    HandleFullRecord handleRecord = new HandleFullRecord(modelHandleRecord.translator, params, returnMap, data,  fullObj);
                                    for (Class<? extends TranslateHandle> handleClass : handleClasses) {
                                        MapUtils.putIfExist(translatorRecord.handleRecordMap, handleClass, handleRecord);
                                    }
                                }
                                returnMap.put(fieldName, value);
                            }
                        }
                    }else {
                        returnMap.put(null,value);
                    }
                }
            }
            return returnMap;
        }
        return translatorRecord.curObj;
    }


}
